package com.enjoy.concurrent.ch1.waitnotify;

/**
 * 快递的实体类
 */
public class Express {
    private static final String CITY = "SHANGHAI";
    private int distance;
    private String site;

    public Express(int km, String site) {
        this.distance = km;
        this.site = site;
    }

    /**
     * 距离变化
     */
    synchronized void changeDistance() {
        this.distance = 101;
        notifyAll();
    }

    /**
     * 地点变化
     */
    synchronized void changeSite() {
        this.site = "SHANGHAI";
        notifyAll();
    }

    /**
     * 线程等待公里数的变化
     */
    synchronized void waitInstance() {
        System.out.println("this.distance = " + this.distance);
        while (this.distance < 100) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("this.distance = " + this.distance);
    }

    synchronized void waitSite() {
        System.out.println("this.site = " + this.site);
        while (!this.site.equals(CITY)) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("this.site = " + this.site);
    }


}
